package com.dymall.api.config;

import com.dymall.api.client.fallback.ItemClientFallbackFactory;
import com.dymall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;


public class DefaultFeignConfig {
    //定义Feign的日志级别
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    //实现微服务之间传递信息时写入请求头header
    //解决问题 —— 提交订单后在执行删除购物车商品时没有传递用户id
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){//请求拦截器
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userInfo = UserContext.getUser();
                if(userInfo != null){
                    template.header("user-info", userInfo.toString());
                }
            }
        };
    }

    /**
     * 将定义好的ItemClientFallbackFactory注册为一个Bean对象
     * @return
     */
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory(){
        return new ItemClientFallbackFactory();
    }
}
